import axios from 'axios';
import qs from 'qs';
import { ElMessage } from 'element-plus';
import { useUserStore } from '@/stores/user';

class HttpRequest {
  // 获取基础配置
  getInsideConfig() {
    const config = {
      baseURL:
        import.meta.env.MODE === 'production'
          ? 'https://fx.a100-cn.com/api'
          : import.meta.env.VITE_APP_CODE_ENV === 'testing'
            // ? 'http://175.6.148.184:18001/api'
            ? '/api'
            : '/api',   
            // http://fx.a100-cn.com/
      timeout: 30000 //请求超时时间
    };
    return config;
  }
    
  // 设定拦截器
  interceptors(instance) {
    // 请求拦截器
    instance.interceptors.request.use(
      config => {
        // 携带token
        if (localStorage.getItem('token')) {
          config.headers.token = localStorage.getItem('token');
        }

        return config;
      },
      err => {
        return Promise.reject(err);
      }
    );

    // 响应拦截器
    instance.interceptors.response.use(
      res => {
        if (res.status === 200) {
          const code = res.data.code;
          if (code === 0) {
            return Promise.resolve(res.data);
          } else {
            if (code === 3002) {
              ElMessage.error('登录已过期，请重新登录');
              const userStore = useUserStore();
              userStore.signOut();
              setTimeout(() => {
                const { origin } = window.location;
                window.location.href = origin + '/pc/login';
              }, 800);
            } else {
              const msg = res.data.msg || '';
              ElMessage.error(msg);

              return Promise.reject(res);
            }
          }
        } else {
          return Promise.reject(res);
        }
      },
      err => {
        if (err.response.status === 401) {
          ElMessage.error('登录已过期，请重新登录');
          const userStore = useUserStore();
          userStore.signOut();
          setTimeout(() => {
            const { origin } = window.location;
            window.location.href = origin + '/pc/login';
          }, 800);
        } else {
          console.log('请求错误');
          ElMessage.error(err.response.data.msg || '请求错误');
        }
        return Promise.reject(err);
      }
    );
  }

  // 创建实例
  request(options) {
    const instance = axios.create();
    const newOptions = Object.assign(this.getInsideConfig(), options);
    this.interceptors(instance);
    return instance(newOptions);
  }

  // get方法
  get(url, data) {
    const options = Object.assign(
      {
        method: 'get',
        url
      },
      data
    );
    return this.request(options);
  }

  // post方法
  post(url, data) {
    return this.request({
      method: 'post',
      url,
      data,
      headers: {
        'Content-Type': 'application/json'
      }
    });
  }

  // put方法
  put(url, data) {
    return this.request({
      method: 'put',
      url,
      data
    });
  }

  // delete方法
  delete(url, data) {
    return this.request({
      method: 'delete',
      url,
      data
    });
  }
}

export default new HttpRequest();
